package com.adam.springsecurity;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@Secured("ROLE_USER")
public class OAuth2Controller {

    @RequestMapping("/oauth2/getUserDetails")
    @ResponseBody
    public Map<String,Object> getUserDetails() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Map<String,Object> resultMap = new HashMap<>();
        if(authentication instanceof OAuth2AuthenticationToken) {
            OAuth2AuthenticationToken oAuth2AuthenticationToken = (OAuth2AuthenticationToken) authentication;
            Collection<? extends GrantedAuthority> authorities = oAuth2AuthenticationToken.getAuthorities();
            List<String> authorityStrings = new LinkedList<>();
            for (GrantedAuthority authority : authorities) {
                authorityStrings.add(authority.getAuthority());
            }
            resultMap.put("authority", authorityStrings);
            String registrationId = oAuth2AuthenticationToken.getAuthorizedClientRegistrationId();
            MyOAuth2User.OAuthProvider oAuthProvider = MyOAuth2User.OAuthProvider.Unknown;
            for(MyOAuth2User.OAuthProvider oap: MyOAuth2User.OAuthProvider.values()) {
                if(oap.name().equalsIgnoreCase(registrationId)) {
                    oAuthProvider = oap;
                    break;
                }
            }

            OAuth2User oAuth2User = oAuth2AuthenticationToken.getPrincipal();
            resultMap.put("provider", oAuthProvider);
            resultMap.put("name", oAuth2User.getName());
            switch (oAuthProvider) {
                case GitHub:
                    resultMap.put("id", oAuth2User.getAttribute("id"));
                    resultMap.put("username", oAuth2User.getAttribute("login"));
                    resultMap.put("avatar_url", oAuth2User.getAttribute("avatar_url"));
                    resultMap.put("realName", oAuth2User.getAttribute("name"));
                    resultMap.put("email", oAuth2User.getAttribute("email"));
                    resultMap.put("bio", oAuth2User.getAttribute("bio"));
                    break;
                case Huawei:
                    resultMap.put("displayName", oAuth2User.getAttribute("displayName"));
                    resultMap.put("headPictureURL", oAuth2User.getAttribute("headPictureURL"));
                    resultMap.put("unionID", oAuth2User.getAttribute("unionID"));
                    resultMap.put("openID", oAuth2User.getAttribute("openID"));
                    resultMap.put("displayNameFlag", oAuth2User.getAttribute("displayNameFlag"));
                    break;
                default:
            }
        }
        return resultMap;
    }

}
